cc.Class({
    extends: cc.Component,

    properties: {
        lbName: cc.Label,
        lbRoom: cc.Label,
        lbLive: cc.Label,
        lvUser: cc.ScrollView,
        lvMsg: cc.ScrollView,
        ipText: cc.EditBox,
        isOpenUserLV: false,
    },

    onLoad: function () {

    },

    onEnable: function () {

        var that = this;

        this.isOpenUserList = false;
        this.setUserList(this.isOpenUserList);

        pomelo.on("onChat", function (data) {
            cc.log("ui_chat.onChat: from %s, to %s, content is %s.", data.from, data.to, data.content);
        });

        pomelo.on("onEnter", function (data) {
            cc.log("ui_chat.onEnter: data is ", data);
            playerMgr.add(data.uid);
            that.setLive();
            if (that.isOpenUserList) {
                that.setUserList(true);
            }
        });

        pomelo.on("onLeave", function (data) {
            cc.log("ui_chat.onLeave: data is ", data);
            playerMgr.remove(data.uid);
            that.setLive();
            if (that.isOpenUserList) {
                that.setUserList(true);
            }
        });

        pomelo.on("disconnect", function (reason) {
            cc.log("ui_chat.disconnect: ", reason);
        });

        pomelo.on("io-error", function (data) {
            cc.log("ui_chat.io-error: ", data);
        });

        this.setMe();
        this.setRoom();
        this.setLive();
    },

    // onDisable: function () {
    //     pomelo.
    // },

    onClickMe: function () {
        pomelo.disconnect();
        uiMgr.close("ui_chat");
        uiMgr.open("ui_login");
    },

    onClickLive: function () {
        this.isOpenUserList = !this.isOpenUserList;
        this.setUserList(this.isOpenUserList);
    },
    onclickGo: function () {
        cc.log("ui_chat.onClickGo: text is ", this.ipText.string);
        var that = this;
        pomelo.request("chat.chatHandler.push", { to: "*", content: this.ipText.string }, function (data) {
            cc.log("ui_chat.onClickGo: resp is ", data);
        });
    },

    setRoom: function () {
        this.lbRoom.string = ui_login.rid;
    },

    setMe: function () {
        this.lbName.string = "Me: " + ui_login.hero;
    },

    setLive: function () {
        this.lbLive.string = "Live: " + playerMgr.players.length;
    },

    setUserList: function (status) {
        var that = this;
        this.lvUser.node.active = status;
        if (status) {
            var root = cc.find("vw/ctt", this.lvUser.node);
            var children = root.children;
            if (children) {
                for (var child of children) {
                    if (child) {
                        child.destroy();
                    }
                }
            }
            var cellHeight = 40;
            for (var i = 0; i < playerMgr.players.length;) {
                var player = playerMgr.players[i];
                var child = new cc.Node(i);
                var lb = child.addComponent(cc.Label);
                lb.string = player.id;
                var btn = child.addComponent(cc.Button);
                btn.node.on("click", function (event) {
                    var selectedPlayer = playerMgr.players[this.name];
                    cc.log("onUserListItemClick: player is ", selectedPlayer);
                    that.ipText.string += "@" + selectedPlayer.id + " ";
                });
                child.parent = root;
                child.width = 220;
                child.height = 40;
                child.anchorX = 0;
                child.anchorY = 0;
                i++;
                child.y = - i * cellHeight;
            }
            root.height = cellHeight * playerMgr.players.length;
        }
    }
});
